<template>
  <el-dialog v-dialogDrag v-if="popenevTVisible" title="退回" :close-on-click-modal="false" :before-close="handleCancel" :visible.sync="popenevTVisible" width="1000px">
    <ReturnList ref="ReturnList" :modulename="moduleName"></ReturnList>
    <div style="margin-top:10px">
      <el-input placeholder="请填写退回原因" type="textarea" :maxlength="400" v-model="thyy" clearable>
      </el-input>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="confirmLoading" @click="handleOk">确定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getList } from "../../api/common/bwgzApi";
import { sendbackDoc, sendDoc } from "../../api/common/flowsend";
import ReturnList from "./ReturnList.vue";
export default {
  data()
  {
    return {
      //流程节点unid
      flowstructureunid: null,
      unid: null,
      returnDocForm: {
        backUnids: []
      },
      //退回节点列表
      callBackNode: [],
      rules: {},
      moduleName: null,
      formType: null,
      //是否显示弹窗
      popenevTVisible: true,
      confirmLoading: false,
      //promise 状态
      promiseStatus: null,
      //退回原因
      thyy: null
    };
  },
  components: { ReturnList },
  methods: {
    showModal(data)
    {
      this.flowstructureunid = data.flowstructureunid;
      this.unid = data.unid;
      this.moduleName = data.moduleName;
      this.formType = data.formType;
      this.$nextTick(() =>
      {
        // 获取办文跟踪数据
        this.$refs.ReturnList.getTableData(this.unid);
      })
      // this.getTableData(this.unid);
      // 创建promise
      return new Promise((resolve, reject) =>
      {
        this.promiseStatus = {
          resolve,
          reject,
        };
      });
    },
    //获取办文数据
    getTableData(unid)
    {
      getList({
        pageSize: 100,
        pageNo: 1,
        wjId: this.unid,
        modulename: this.moduleName
      }).then(res =>
      {
        let tampArr = res.result.data || [];
        this.callBackNode = [];
        tampArr.forEach(item =>
        {
          if ((item.state == 4 || item.state == 2) && item.senderIdent)
          {
            this.callBackNode.push(item);
          }
        })
      })
    },
    handleOk()
    {
      this.confirmLoading = true;
      let records = this.$refs.ReturnList.records;
      this.confirmLoading = true;
      if (records.length < 1)
      {
        this.$message.error("请选择需要退回节点");
        this.confirmLoading = false;
        return;
      }
      let targetNodesWithUser = {};
      records.forEach(pre_lcgz =>
      {
        if (!targetNodesWithUser[pre_lcgz.flowStructureNodeIdent])
        {
          targetNodesWithUser[pre_lcgz.flowStructureNodeIdent] = [];
        }
        targetNodesWithUser[pre_lcgz.flowStructureNodeIdent].push(pre_lcgz.ownerIdent);
      });

      const urlparam = {
        flowstructureunid: this.flowstructureunid,
        P_Unid: this.unid,
        unid: this.unid,
        awakeMode: 0,
        // backlogUrl: this.moduleurl,
        bt: "",
        backlogUrl: this.$route.path,
        finalizeUnFinishTasks: 0,
        moduleName: this.moduleName,
        openBacklogTarget: "_blank",
        targetNodesWithUser: JSON.stringify(targetNodesWithUser),
        sslb: this.formType ? this.formType : "",
        thyy: this.thyy
      };

      this.$confirm("此操作将进行退回, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(res =>
      {
        sendDoc(urlparam).then(res =>
        {
          this.$message.success("退回成功");
          this.destroyElement();
          this.promiseStatus.resolve(res);
        }).catch(() =>
        {

        }).finally(() =>
        {
          this.confirmLoading = false;
        });
      }).catch(() =>
      {
        this.confirmLoading = false;
      });
    },
    handleCancel()
    {
      //清空勾选项
      this.promiseStatus.reject({
        msg: "取消",
      });
      this.destroyElement();
    },
    /**
     * cs 2022-4-8
     * 销毁组件
     */
    destroyElement()
    {
      this.popenevTVisible = false;
    },
  },
};
</script>
